<? 
	include('base.php');
	User::protect();
	$section = 'media';
	include_class('general_media');
	include_class('m2');
	$id = ($_GET['id'] > 0 && is_numeric($_GET['id'])) ? $_GET['id'] : 0;
	if ($id == 0) {
		$id = ($_REQUEST['area'] > 0) ? $_REQUEST['area'] : 0;
	}
	$gm = new GeneralMedia();
	if ($id != 0) {
		$ma = MediaArea::get($id);
		$gm = new GeneralMedia();
		if (is_object($ma)) {
			$isNotGeneralMedia = !$ma->isGeneralMedia();
			$gm->setAreaObject($ma);
		}
	}

	if ($_POST['localfile']) {
		if ($ma) {
			$doSubmit = true;
			$res = $ma->addMediaUpload('mediafile', $gm);
		}
	} else if ($_POST['url']) {
		if ($ma) {
			$doSubmit = true;
			$res = $ma->addMediaRemote($_POST['url'], $gm);
		}
	} else if ($_POST['toAdd'] == 'all') { 
		if ($ma) {
			$doSubmit = true;
			$keepCopy = ($_POST['copyFromIncoming']) ? 1 : 0;
			$res = $ma->addAllMediaIncoming($keepCopy, $gm);
		}
	} else if ($_POST['incoming_file']) {
		if ($ma) {
			$doSubmit = true;
			$keepCopy = ($_POST['copyFromIncoming']) ? 1 : 0;
			$res = $ma->addMediaIncoming($_POST['incoming_file'], $keepCopy, $gm);
			if (!db::isError($response) && $_POST['toAdd'] == 'all') {
				header('Location: media.php?id=' . $_GET['id']);
				exit;
			}
		}
	}
	
	if ($doSubmit) {
		
		if (!db::isError($res) && $_POST['toAdd'] == 'all') {
			header('Location: media.php?id=' . $_GET['id']);
			exit;
		} else if (!db::isError($res)) {
			header('Location: media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' . $res);
			exit;
		}
	}
	
	$page_title = 'Add Media';
	include('layout/header.php');

?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="media.php">Media&nbsp;&#62;</a>
<? 
	if (is_object($ma)) {
		$breadcrumb = $ma->getAreaTrail();
		if ($breadcrumb) { 
			foreach($breadcrumb as $maBC) {
				echo('<a href="media.php?id=' . $_GET['id'] . '">' . $maBC->getTitle() . '&nbsp;&#62;&nbsp;</a>');
			}
		}
	}	
	echo("Edit Media Area");
?>
</div>

<? if (db::isError($gm)) { ?>

	<?=$gm->outputDialog('Return to Media','media.php');?>

<? } else if (!db::isError($ma)) { 
	if (db::isError($res)) {
		$res->outputList();
	}
	?>

	<? include('layout/media_add.php'); ?>

<? } else { ?>

	<?=$ma->outputDialog('Return to Media','media.php');?>
		
<? } ?>

<? include('layout/footer.php'); ?>
